package com.wing.home.service;

import com.wing.common.utils.DateUtils;
import com.wing.home.dao.HomeDao;
import com.wing.home.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 主页
 *
 * @author xiayang
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Resource
    HomeDao homeDao;

    @Override
    public List<PurchaseTrendNumber> purchaseTrendNumTotal(Map<String, Object> params) {
        params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
        params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        List<PurchaseTrendNumber> purchaseTrendNumberList = homeDao.purchaseTrendNumTotal(params);
        List<PurchaseTrendNumber> resultList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            String purchaseDate = DateUtils.getDateByDay(new Date(), i == 0 ? 0 : -i);
            PurchaseTrendNumber purchaseTrendNumber = purchaseTrendNumberList.stream().filter(t -> t.getPurchaseDate().equals(purchaseDate)).findFirst().orElse(null);
            if (purchaseTrendNumber == null) {
                purchaseTrendNumber = new PurchaseTrendNumber();
                purchaseTrendNumber.setPurchaseDate(purchaseDate);
                purchaseTrendNumber.setPurchaseNum(0);
            }
            resultList.add(purchaseTrendNumber);
        }
        return resultList;
    }

    @Override
    public List<PurchaseTrendAmount> purchaseTrendAmountTotal(Map<String, Object> params) {
        params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
        params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        List<PurchaseTrendAmount> purchaseTrendAmountList = homeDao.purchaseTrendAmountTotal(params);
        List<PurchaseTrendAmount> resultList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            String purchaseDate = DateUtils.getDateByDay(new Date(), i == 0 ? 0 : -i);
            PurchaseTrendAmount purchaseTrendAmount = purchaseTrendAmountList.stream().filter(t -> t.getPurchaseDate().equals(purchaseDate)).findFirst().orElse(null);
            if (purchaseTrendAmount == null) {
                purchaseTrendAmount = new PurchaseTrendAmount();
                purchaseTrendAmount.setPurchaseDate(purchaseDate);
                purchaseTrendAmount.setPurchaseAmount(BigDecimal.ZERO);
            }
            resultList.add(purchaseTrendAmount);
        }
        return resultList;
    }

    @Override
    public WarehouseTotal warehouseTotal(Map<String, Object> params) {
        WarehouseTotal warehouseTotal = new WarehouseTotal();
        if (StringUtils.isNotBlank(params.get("startTime").toString()) && StringUtils.isNotBlank(params.get("endTime").toString())) {
            //库存总量
            warehouseTotal.setInventoryTotal(homeDao.warehouseTotalByInventory(params));
            //物料数
            warehouseTotal.setMaterialTypeTotal(homeDao.warehouseTotalByMaterial(params));
            //入库数量
            params.put("inventoryType", 1);
            warehouseTotal.setInStorageTotal(homeDao.warehouseTotalByInOut(params));
            //出库数量
            params.put("inventoryType", 2);
            warehouseTotal.setOutStorageTotal(homeDao.warehouseTotalByInOut(params));
        } else if (params.get("queryType") == null || Integer.valueOf(params.get("queryType").toString()) == 3) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), 0));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
            //库存总量
            warehouseTotal.setInventoryTotal(homeDao.warehouseTotalByInventory(params));
            //物料数
            warehouseTotal.setMaterialTypeTotal(homeDao.warehouseTotalByMaterial(params));
            //入库数量
            params.put("inventoryType", 1);
            warehouseTotal.setTodayInStorageTotal(homeDao.warehouseTotalByInOut(params));
            //出库数量
            params.put("inventoryType", 2);
            warehouseTotal.setTodayOutStorageTotal(homeDao.warehouseTotalByInOut(params));
        } else if (Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
            //库存总量
            warehouseTotal.setInventoryTotal(homeDao.warehouseTotalByInventory(params));
            //物料数
            warehouseTotal.setMaterialTypeTotal(homeDao.warehouseTotalByMaterial(params));
            //入库数量
            params.put("inventoryType", 1);
            warehouseTotal.setWeekInStorageTotal(homeDao.warehouseTotalByInOut(params));
            //出库数量
            params.put("inventoryType", 2);
            warehouseTotal.setWeekOutStorageTotal(homeDao.warehouseTotalByInOut(params));
        } else if (Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
            //库存总量
            warehouseTotal.setInventoryTotal(homeDao.warehouseTotalByInventory(params));
            //物料数
            warehouseTotal.setMaterialTypeTotal(homeDao.warehouseTotalByMaterial(params));
            //入库数量
            params.put("inventoryType", 1);
            warehouseTotal.setMonthInStorageTotal(homeDao.warehouseTotalByInOut(params));
            //出库数量
            params.put("inventoryType", 2);
            warehouseTotal.setMonthOutStorageTotal(homeDao.warehouseTotalByInOut(params));
        }
        return warehouseTotal;
    }

    @Override
    public List<InventoryInOutNum> inventoryInOutTotal(Map<String, Object> params) {
        if (StringUtils.isNotBlank(params.get("startTime").toString()) && StringUtils.isNotBlank(params.get("endTime").toString())) {

        } else if (params.get("queryType") == null || Integer.valueOf(params.get("queryType").toString()) == 3 || Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
        }
        //库存出入库统计
        List<InventoryInOutTotal> inventoryInOutTotalList = homeDao.inventoryInOutTotal(params);
        List<InventoryInOutNum> resultList = new ArrayList<>();
        //时间间隔天数
        Integer dayNum = DateUtils.getTimeDay(params.get("startTime").toString(), params.get("endTime").toString());
        dayNum = dayNum == 1 ? 6 : dayNum;
        for (int i = dayNum; i >= 0; i--) {
            Date endTime = DateUtils.stringFormatDate(params.get("endTime").toString(), DateUtils.DATE_PATTERN);
            String inOutInventoryDate = DateUtils.getDateByDay(endTime, i == 0 ? 0 : -i);
            InventoryInOutNum inventoryInOutNum = new InventoryInOutNum();
            inventoryInOutNum.setInoutStorageDate(inOutInventoryDate);
            //入库
            InventoryInOutTotal inventoryInTotal = inventoryInOutTotalList.stream().filter(t -> t.getInoutStorageDate().equals(inOutInventoryDate) && t.getInventoryType() == 1).findFirst().orElse(null);
            if (inventoryInTotal == null) {
                inventoryInOutNum.setInStorageNum(0);
            } else {
                inventoryInOutNum.setInStorageNum(inventoryInTotal.getInOutStorageNum());
            }
            //出库
            InventoryInOutTotal inventoryOutTotal = inventoryInOutTotalList.stream().filter(t -> t.getInoutStorageDate().equals(inOutInventoryDate) && t.getInventoryType() == 2).findFirst().orElse(null);
            if (inventoryOutTotal == null) {
                inventoryInOutNum.setOutStorageNum(0);
            } else {
                inventoryInOutNum.setOutStorageNum(inventoryOutTotal.getInOutStorageNum());
            }
            resultList.add(inventoryInOutNum);
        }
        return resultList;
    }

    @Override
    public List<InOutStorageMaterialTop> inOutStorageMaterialTop(Map<String, Object> params) {
        if (StringUtils.isNotBlank(params.get("startTime").toString())
                && StringUtils.isNotBlank(params.get("endTime").toString())) {
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 3) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), 0));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
        }
        return homeDao.inOutStorageMaterialTop(params);
    }

    @Override
    public List<InOutStorageTypeTop> inOutStorageTypeTop(Map<String, Object> params) {
        if (StringUtils.isNotBlank(params.get("startTime").toString())
                && StringUtils.isNotBlank(params.get("endTime").toString())) {
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 3) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), 0));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
        }
        return homeDao.inOutStorageTypeTop(params);
    }

    @Override
    public List<ProductTypeTotal> productTypeTotal(Map<String, Object> params) {
        if (StringUtils.isNotBlank(params.get("startTime").toString())
                && StringUtils.isNotBlank(params.get("endTime").toString())) {
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 3) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), 0));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
        }
        return homeDao.productTypeTotal(params);
    }

    @Override
    public List<WarehouseTypeTotal> warehouseTypeTotal(Map<String, Object> params) {
        if (StringUtils.isNotBlank(params.get("startTime").toString())
                && StringUtils.isNotBlank(params.get("endTime").toString())) {
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 3) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), 0));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 2) {
            params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
            params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        } else if (params.get("queryType") != null && Integer.valueOf(params.get("queryType").toString()) == 1) {
            params.put("startTime", DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN));
            params.put("endTime", DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN));
        }
        return homeDao.warehouseTypeTotal(params);
    }
}
